#!coding=utf-8
from common import getAccToken
from t4weixin.common import WEIXIN_RESPONSE_CODE
from t4weixin.models import WMenu
import json
import urllib2

def sendmenu():
    access_token=getAccToken()
    # print access_token  cd ..
    posturl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token  
    
    menu = {'button':[]}
    
    def make_click(node):
        return {
                   'type':'click',
                   'name':node.name,
                   'key':node.value
                   }
    
    def make_view(node):
        return {
                   'type':'view',
                   'name':node.name,
                   'url':node.value
                   }
    
    for root in WMenu.objects.root_nodes():
        if root.wtype == 'click':
            menu['button'].append(
                                  make_click(root)
                                  )
        elif  root.wtype == 'view':
            menu['button'].append(
                                  make_view(root)
                                  )
        elif root.wtype == 'parent':
            data = []
            for sub_node in root.get_children():
                if sub_node.wtype == 'click':
                    data.append(
                                  make_click(sub_node)
                                  )
                elif  sub_node.wtype == 'view':
                    data.append(
                                  make_view(sub_node)
                                  )
            menu['button'].append(
                                  {
                                   'name':root.name,
                                   'sub_button':data
                                   }
                                  )
    req=urllib2.urlopen(posturl, json.dumps(menu,ensure_ascii=False).encode('utf-8'))  
      
    result = json.loads(req.read())
    if result['errcode'] != 0:
        return False, WEIXIN_RESPONSE_CODE[str(result['errcode'])]
    return True,''
     